/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/14
 * Time: 03:52
 */
$(function(){

    $('.upload-img-post').ajaxUpload({
        url: '/v1/upload',
        name:'standard_post',
        onSubmit:function (data){
            $('#text-logo').html('正在上传中...');
            return true;
        },
        onComplete:function (json){
            if(json.status==200){
                var info = json.body.info[0];
                $('.upload-img-post>img').attr('src','/pictures/' + info.savename);
                $('.input-standard-post').val(info.savename);
                $('#text-logo').html('上传图片');
            }else{
                BootstrapDialog.show({
                    title: '温馨提示',
                    message: '<div class="empty">'+ json.message +'</div>',
                    cssClass: 'modal-lol-ui'
                });
            }
        }
    });

    sendAjax('/v1/manager/standard/detail.json', 'get', {id:id}, function(json) {
        var data = json.body.data;
        if (data) {
            $('.standard-name').val(data.name);
            $('.input-standard-post').val(data.post);
            if (data.post != '') {
                $('.img-standard-post').attr('src', '/pictures/'+data.post);
            }
            $('.standard-desc').val(data.desc);
        }
    });

    $('.btn-save').on('click', function() {
        var data = {
            id : $('.standard-id').val(),
            name : $('.standard-name').val(),
            post : $('.input-standard-post').val(),
            desc : $('.standard-desc').val()
        };
        if (checkForm(data)) {
            sendAjax('/v1/manager/update/standard', 'post', {data:data}, function(json) {
                var div = '<div><input type="hidden" class="jump-url" value="/manager/apart/standard.html">' + json.message + '</div>';
                $('#action-tip .modal-body .tip-content').html(div);
                $('#action-tip').modal('show');
            });
        }
    });

    $('#action-tip').on('hide.bs.modal', function(){
        if ($(this).find('.jump-url').val()) {
            location.href = $(this).find('.jump-url').val();
        }
        $(this).find('.tip-content').text('');
    });
});

function checkForm(data) {
    if (!data.id) {
        $('#action-tip .modal-body .tip-content').text('非法请求');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.name) {
        $('#action-tip .modal-body .tip-content').text('请填写户型名称');
        $('#action-tip').modal('show');
        return false;
    }
    return true;
}